using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Uow;
using Volo.Abp.Data;
using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using Joy.Erp.BasicArchives.Core.Events;

namespace Joy.Erp.BasicArchives.ItemMasters
{
    public class UnitDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        protected IDistributedEventBus DistributedEventBus { get; }
        protected IDataFilter<ISoftDelete> SoftDeleteFilter { get; }
        protected IUnitRepository UnitRepository { get; }
        protected IGuidGenerator GuidGenerator { get; }
        protected ICurrentTenant CurrentTenant { get; }

        public UnitDataSeedContributor(
            IDistributedEventBus distributedEventBus,
            IDataFilter<ISoftDelete> softDeleteFilter,
            IUnitRepository unitRepository,
            IGuidGenerator guidGenerator,
            ICurrentTenant currentTenant)
        {
            DistributedEventBus = distributedEventBus;
            SoftDeleteFilter = softDeleteFilter;
            UnitRepository = unitRepository;
            GuidGenerator = guidGenerator;
            CurrentTenant = currentTenant;
        }

        [UnitOfWork]
        public async Task SeedAsync(DataSeedContext context)
        {
            using (CurrentTenant.Change(context?.TenantId))
            {
                //await SeedUnitsAsync(context?.TenantId, "BA11", "吨");
                //await SeedUnitsAsync(context?.TenantId, "BA12", "千克");
                //await SeedUnitsAsync(context?.TenantId, "BA13", "克");
                //await SeedUnitsAsync(context?.TenantId, "BA21", "米");
                //await SeedUnitsAsync(context?.TenantId, "BA22", "厘米");
                //await SeedUnitsAsync(context?.TenantId, "BA23", "毫米");
                //await SeedUnitsAsync(context?.TenantId, "BA31", "日");
                //await SeedUnitsAsync(context?.TenantId, "BA32", "时");
                //await SeedUnitsAsync(context?.TenantId, "BA33", "分");
                //await SeedUnitsAsync(context?.TenantId, "BA34", "秒");
                //await SeedUnitsAsync(context?.TenantId, "BA41", "箱");
                //await SeedUnitsAsync(context?.TenantId, "BA42", "盒");
                //await SeedUnitsAsync(context?.TenantId, "BA43", "包");
                //await SeedUnitsAsync(context?.TenantId, "BA44", "个");
                //await SeedUnitsAsync(context?.TenantId, "BA45", "只");
                //await SeedUnitsAsync(context?.TenantId, "BA51", "平方米");
                //await SeedUnitsAsync(context?.TenantId, "BA52", "平方分米");
                //await SeedUnitsAsync(context?.TenantId, "BA61", "立方米");
                //await SeedUnitsAsync(context?.TenantId, "BA62", "立方分米");
            }
        }

        private async Task SeedUnitsAsync(Guid? tenantId, string code, string displayName)
        {
            using (SoftDeleteFilter.Disable())
            {
                var exists = await UnitRepository.FirstOrDefaultAsync(x => x.TenantId == tenantId && x.Code == code && x.DisplayName == displayName);
                if (exists == null)
                {
                    var unit = new Unit();
                    unit.SetId(GuidGenerator.Create());
                    unit.Code = code;
                    unit.DisplayName = displayName;

                    await DistributedEventBus.PublishAsync(new EntityCreatingEto<Unit>(unit), false);
                    await UnitRepository.InsertAsync(unit, true);
                }
            }
        }
    }
}